package com.test.exception;

/**
 * 通用状态码和返回消息体封装
 */
public class CodeMsg {
    //自定义状态码
    private int code;
    //消息消息
    private String msg;

    //通用异常 4001XX
    public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端口异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(400101, "参数校验异常:%s");

    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }

    public CodeMsg() {
    }

    /**
     * 模块一 4002XX
     * 模块二 4003XX
     * 模块三 4004XX
     *
     * @param code 状态码
     * @param msg  消息体
     */
    public CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "CodeMsg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
}
